package com.example.liucxu.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定义进度环View
 * Created by liucxu on 2018/2/2.
 */

public class CustomProgressView extends View {

    private int custom_progress_main_color;
    private int custom_progress_progress_color;
    private int custom_progress_progress_stroke_width;
    private int custom_progress_progress_speed;

    private Paint mPaint;
    private int progress = 20;

    public CustomProgressView(Context context) {
        this(context, null);
    }

    public CustomProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressView);
        if (typedArray != null) {
            custom_progress_main_color = typedArray.getColor(R.styleable.CustomProgressView_custom_progress_main_color, Color.GRAY);
            custom_progress_progress_color = typedArray.getColor(R.styleable.CustomProgressView_custom_progress_progress_color, Color.BLUE);
            custom_progress_progress_stroke_width = typedArray.getDimensionPixelSize(R.styleable.CustomProgressView_custom_progress_progress_stroke_width, 30);
            custom_progress_progress_speed = typedArray.getInt(R.styleable.CustomProgressView_custom_progress_progress_speed, 10);
            typedArray.recycle();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(20);
                        postInvalidate();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int center_x = getWidth() / 2;
        int radius = center_x - custom_progress_progress_stroke_width / 2;
        if (progress == 360) {
            int local_middle = custom_progress_main_color;
            custom_progress_main_color = custom_progress_progress_color;
            custom_progress_progress_color = local_middle;
            progress = 0;
        }
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(custom_progress_progress_stroke_width);
        mPaint.setColor(custom_progress_main_color);
        mPaint.setAntiAlias(true);
        progress = progress + 1;
        canvas.drawCircle(center_x, center_x, radius, mPaint);
        mPaint.setColor(custom_progress_progress_color);
        RectF rectf = new RectF(custom_progress_progress_stroke_width / 2, custom_progress_progress_stroke_width / 2, 2 * center_x - custom_progress_progress_stroke_width / 2, 2 * center_x - custom_progress_progress_stroke_width / 2);
        canvas.drawArc(rectf, -90, progress, false, mPaint);
    }
}
